/*
 *  Copyright 2011-2012 Redia A/S
 * 
 *  This file is part of Redias Android Utilities.
 *
 *  Redias Android Utilities is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redias Android Utilities is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redias Android Utilities.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.lib.log;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

import dk.redia.lib.utils.AppConfiguration;

import android.os.Environment;

public class Log {
	
	public static final boolean fileLoggingEnabled = false;
	public static FileWriter writer;
	
	static {
		try {
			writer = new FileWriter(Environment.getExternalStorageDirectory().getPath() + "/redia-log.txt", true);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static void fileLog(String tag, String msg) {
		if (!fileLoggingEnabled) {
			return;
		}
		try {
			writer.write("[" +new Date() + "][" + tag + "] " + msg + "\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	public static void d(String tag, String msg) {
		if (AppConfiguration.getInstance().getProductionMode()) {
			android.util.Log.d(tag, msg);
			fileLog(tag, msg);
		}
	}
	
	public static void w(String tag, String msg) {
		if (AppConfiguration.getInstance().getProductionMode()) {
			android.util.Log.w(tag, msg);
			fileLog(tag, msg);
		}
	}
	
	public static void e(String tag, String msg) {
		if (AppConfiguration.getInstance().getProductionMode()) {
			android.util.Log.e(tag, msg);
			fileLog(tag, msg);
		}
	}
	
}
